package com.yiqi.zhiyuan.common.widget.transition;

import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;

import com.suozhang.framework.framework.AM;
import com.suozhang.framework.utils.DateUtil;
import com.suozhang.framework.utils.RxClipboardTool;
import com.suozhang.framework.utils.T;
import com.yiqi.zhiyuan.R;
import com.yiqi.zhiyuan.common.utils.AppUtil;
import com.yiqi.zhiyuan.entity.vo.OrderVO;
import com.yiqi.zhiyuan.feature.order.view.EndTaskActivity;
import com.yiqi.zhiyuan.feature.order.view.OrderInfoActivity;
import com.yiqi.zhiyuan.feature.order.view.OrderTripingActivity;

import java.util.ArrayList;
import java.util.List;

public class CardPagerAdapter extends PagerAdapter implements CardAdapter {

    private List<CardView> mViews;
    private List<OrderVO> mData;
    private float mBaseElevation;
    Boolean flag = true;
    public CardPagerAdapter() {
        mData = new ArrayList<>();
        mViews = new ArrayList<>();
    }

    public void addCardItem(OrderVO item) {
        mViews.add(null);
        mData.add(item);
    }

    public float getBaseElevation() {
        return mBaseElevation;
    }

    @Override
    public CardView getCardViewAt(int position) {
        return mViews.get(position);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public List<OrderVO> getData() {
        return mData;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(container.getContext())
                .inflate(R.layout.item_home_car, container, false);
        container.addView(view);
        bind(mData.get(position), view);
        CardView cardView = view.findViewById(R.id.cardView);

        if (mBaseElevation == 0) {
            mBaseElevation = cardView.getCardElevation();
        }
        cardView.setMaxCardElevation(mBaseElevation * MAX_ELEVATION_FACTOR);
        mViews.set(position, cardView);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        mViews.set(position, null);
    }

    private void bind(OrderVO item, View view) {
        TextView titleTextView = view.findViewById(R.id.tv_card_order_time);
        TextView tvDeparture = view.findViewById(R.id.tv_card_order_departure);
        TextView tvDestination = view.findViewById(R.id.tv_card_order_destination);
        TextView tv_remark = view.findViewById(R.id.tv_order_remark);
        TextView tvPoint1 = view.findViewById(R.id.tv_card_order_waypoint1);
        TextView tvPoint2 = view.findViewById(R.id.tv_card_order_waypoint2);
        TextView tvDistance = view.findViewById(R.id.tv_card_order_distance);
        TextView tvDriverToPasDistance=view.findViewById(R.id.tv_driverToPasDistance);
        TextView tvFlightno=view.findViewById(R.id.tv_card_order_flightno);

        ImageView imageView=view.findViewById(R.id.btn_order_look_up);
        tvDeparture.setText(item.getDeparture());
        tvDestination.setText(item.getDestination());

        if (item.getUseCarType() == 1) {//接机单
            tvFlightno.setVisibility(View.VISIBLE);
            titleTextView.setText(item.getFlightTime());
            tvFlightno.setText("航班号:" + item.getFlightNo());
        } else if (item.getUseCarType() == 0) {//用车
            tvFlightno.setVisibility(View.GONE);
            if (item.getOrderType() == 0) {//现在
                titleTextView.setText(DateUtil.stringDayString(item.getCreateTime()));
            } else if (item.getOrderType() == 1) {//预约
                titleTextView.setText(TextUtils.isEmpty(item.getReserveTime()) ? "" : DateUtil.stringDayString(item.getReserveTime()));
            } else if (item.getOrderType() == 2) {//代叫
                if (item.getOrderFlag() == 0) {//现在出发
                    titleTextView.setText(DateUtil.stringDayString(item.getCreateTime()));
                } else if (item.getOrderFlag() == 1) {//预约时间
                    titleTextView.setText(DateUtil.stringDayString(item.getReserveTime()));
                }
            }
        } else if (item.getUseCarType() == 2) {
            tvFlightno.setVisibility(View.GONE);
            if (item.getOrderType() == 0) {//现在出发
                titleTextView.setText(DateUtil.stringDayString(item.getCreateTime()));
            } else if (item.getOrderType() == 1) {//预约时间
                titleTextView.setText(DateUtil.stringDayString(item.getReserveTime()));
            }
        }

        tv_remark.setText(TextUtils.isEmpty(item.getMessages()) ? "暂无备注" : item.getMessages());
        tvPoint1.setVisibility(TextUtils.isEmpty(item.getWaypoint1()) ? View.GONE : View.VISIBLE);
        tvPoint2.setVisibility(TextUtils.isEmpty(item.getWaypoint2()) ? View.GONE : View.VISIBLE);
        tvPoint1.setText(TextUtils.isEmpty(item.getWaypoint1()) ? "" : "途径点：" + item.getWaypoint1());
        tvPoint2.setText(TextUtils.isEmpty(item.getWaypoint2()) ? "" : "途径点：" + item.getWaypoint2());
        tvDistance.setText("全程预计" + item.getDistance() + "公里");
        tvDriverToPasDistance.setText(AppUtil.formatDouble(item.getDriverToPasDistance()));
        tvFlightno.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RxClipboardTool.copyText(AM.app(),item.getFlightNo());
                T.showShort("航班号已复制");
            }
        });


        tv_remark.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                int lineCount = tv_remark.getLineCount();
                if (lineCount > 1) {
                    tv_remark.setLines(1);
                    imageView.setVisibility(View.VISIBLE);
                } else {
                    imageView.setVisibility(View.GONE);
                }
                imageView.getViewTreeObserver().removeOnPreDrawListener(this);
                return false;
            }
        });
        imageView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (flag) {
                    flag = false;
                    tv_remark.setEllipsize(null); // 展开
                    tv_remark.setSingleLine(flag);
                    imageView.setImageResource(R.mipmap.ic_brand_down);
                } else {
                    flag = true;
                    tv_remark.setEllipsize(TextUtils.TruncateAt.END); // 收缩
                    tv_remark.setMaxLines(1);
                    imageView.setImageResource(R.mipmap.ic_brand_up);
                }
            }
        });
    }

}
